package org.msfj.admin.org.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.msfj.admin.model.Dept;
import org.msfj.admin.org.mapper.DeptMapper;
import org.msfj.admin.org.mapper.UserMapper;
import org.msfj.admin.org.mapper.UserRoleMapper;
import org.msfj.admin.org.service.DeptService;
import org.msfj.admin.org.service.UserService;
import org.msfj.admin.vo.DeptVo;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 系统部门信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "dept")
public class DeptServiceImpl extends BaseServiceImpl<Dept> implements DeptService {

    @Autowired
    DeptMapper deptMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 根据条件分页查询系统部门信息列表
     *
     * @param deptVo
     * @param page   当前第几页
     * @param rows   每页多少条记录
     * @return PageInfo<Dept>
     */
    @Override
    //@Cacheable
    public PageInfo<DeptVo> getByDept(DeptVo deptVo, int page, int rows) {
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        if (deptVo != null) {
            if (StrUtil.isNotBlank(deptVo.getName())) {
                criteria.andLike("name", "%".concat(deptVo.getName()).concat("%"));
                example.or().andLike("shortName", "%".concat(deptVo.getName()).concat("%"));
            }
        }
        PageHelper.startPage(page, rows);
        List<Dept> deptList = super.getByExample(example);
        PageInfo deptVoPageInfo = new PageInfo(deptList);
        deptVoPageInfo.setList(setOtherProperties(deptList));
        return deptVoPageInfo;
    }

    /**
     * 根据条件查询所有系统部门信息对象列表
     *
     * @param deptVo
     * @return List<Dept>
     */
    @Override
    //@Cacheable
    public List<DeptVo> getAllDept(DeptVo deptVo) {
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        example.setOrderByClause("sort_no asc");
        List<DeptVo> deptVoList = setOtherProperties(super.getByExample(example));
        return deptVoList;
    }

    /**
     * 根据条件查询系统部门信息的tree对象
     *
     * @return
     */
    @Override
    //@Cacheable
    public List<DeptVo> getDeptTree() {
        List<DeptVo> deptVos = this.getAllDept(null);
        List<DeptVo> rootDepts = CollectionUtil.newArrayList();
        if (CollectionUtil.isNotEmpty(deptVos)) {
            for (DeptVo deptVo : deptVos) {
                deptVo.setLabel(deptVo.getName());
                if(StrUtil.isNotBlank(deptVo.getTreePosition())){
                    String[] split = deptVo.getTreePosition().split("&");
                    deptVo.setLevel(split.length);
                }else{
                    deptVo.setLevel(1);
                }
                if (ObjectUtil.equal(deptVo.getParentId(), "0")) {
                    rootDepts.add(deptVo);
                }
                for (DeptVo childVo : deptVos) {
                    if (ObjectUtil.equal(childVo.getParentId(), deptVo.getId())) {
                        if (ObjectUtil.isNull(deptVo.getChildren())) {
                            List<DeptVo> children = CollectionUtil.newArrayList();
                            children.add(childVo);
                            deptVo.setChildren(children);
                        } else {
                            deptVo.getChildren().add(childVo);
                        }
                    }
                }
            }
        }
        return rootDepts;
    }

//    /**
//     * 删除部门:1 查询要删除的部门及子部门  2 根据所有的部门查询用户信息并删除  3 根据用户id删除关联的角色表信息
//     *
//     * @param dept
//     * @return
//     */
////    @Override
////    @Transactional
////    public DeptVo delDeptSetIdCount(Dept dept) {
////        DeptVo deptVo = new DeptVo();
////        BeanUtils.copyProperties(dept, deptVo);
////        deptVo.setId(dept.getId());
////        List<DeptVo> allDept = this.getAllDept(deptVo);
////        List<String> allDeptId = CollectionUtil.newArrayList();
////        allDeptId.add(dept.getId());
////        for (DeptVo vo : allDept) {
////            allDeptId.add(vo.getId());
////        }
////        super.delById(dept);
////        Example example = new Example(User.class);
////        Example.Criteria criteria = example.createCriteria();
////        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
////        criteria.andIn("deptId", allDeptId);
////        List<User> byExample = userService.getByExample(example);
////        List<String> allUserId = CollectionUtil.newArrayList();
////        if (CollectionUtil.isNotEmpty(byExample)) {
////            for (User user : byExample) {
////                allUserId.add(user.getId());
////                user.setUpdateTime(new Date());
////                user.setEnable(Enable.DELETED.getCode());
////                userMapper.updateByPrimaryKeySelective(user);
////            }
////            Example exampleUserRole = new Example(UserRole.class);
////            exampleUserRole.createCriteria().andIn("userId", allUserId);
////            userRoleMapper.deleteByExample(exampleUserRole);
////        }
////        return deptVo;
////    }

    /**
     * 设置vo的其他属性属性
     *
     * @param dept
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public DeptVo setOtherProperties(Dept dept) {
        DeptVo deptVo = new DeptVo();
        BeanUtils.copyProperties(dept, deptVo);
        return deptVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param depts
     * @return List<DeptVo>
     */
    @Override
    //@Cacheable
    public List<DeptVo> setOtherProperties(List<Dept> depts) {
        List<DeptVo> deptVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(depts)) {
            for (Dept dept : depts) {
                DeptVo deptVo = new DeptVo();
                BeanUtils.copyProperties(dept, deptVo);
                deptVos.add(deptVo);
            }
        }
        return deptVos;
    }
}
